package cn.yunhe.factory;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
 * SQLsession工具类
 * @author Administrator
 *
 */
public class SqlSessionFactoryUtil {
       private static SqlSessionFactory factory;
       private SqlSessionFactoryUtil(){
    	 
       }
       static{
    	   Reader reader=null;	   
    	   try {
    		   //读取主配置文件
    		   reader=Resources.getResourceAsReader("mybatis/mybatis.xml");
			   //根据主配置文件创建sessionFactory
    		   factory=new SqlSessionFactoryBuilder().build(reader);
		} catch (Exception e) {
             e.printStackTrace();
		}finally {
			if(reader!=null){
			    try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
    	       	   
       }
       /**
        * 获得session
        * @return
        */
       public static SqlSession getSqlSession(){
		   return factory.openSession();    	   
       }

}

